//
//  FirstViewController.m
//  UI_豆瓣扩展
//
//  Created by dllo on 15/11/8.
//  Copyright © 2015年 张磊. All rights reserved.
//

#import "FirstViewController.h"
#import "FirstView.h"
#import "SecondViewController.h"
#import "DataBaseUser.h"
#import "UserModle.h"
@interface FirstViewController ()<firstViewDelegate,secondVieewDelegate>

@property (nonatomic, retain)FirstView *firstV;

@end

@implementation FirstViewController

- (void)dealloc
{
    [_firstV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.firstV = [[FirstView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.view addSubview:self.firstV];
    [_firstV release];
    
    self.navigationItem.title = @"用户登陆";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftbar)];
    
    self.firstV.firstVDelegate = self;
    self.firstV.secondVDelegate = self;
    
    [[DataBaseUser shareDataBase]openDB];
    [[DataBaseUser shareDataBase]createTable];
    
}

-(void)setButtonToFirstVC
{
    UserModle *user = [[UserModle alloc]init];
    user = [[DataBaseUser shareDataBase]selectUserName:self.firstV.nameTextFild.text];
    
    if ([self.firstV.nameTextFild.text isEqualToString:user.userName] && [self.firstV.passwordTextFild.text isEqualToString:user.password]) {
        [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"isLogin"];
        [[NSUserDefaults standardUserDefaults]setObject:self.firstV.nameTextFild.text forKey:@"firstName"];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"登陆成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *othact = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            NSLog(@"OK");
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        [alert addAction:othact];
        [self presentViewController:alert animated:YES completion:nil];
    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登陆失败" message:@"用户名或密码不正确" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *canact = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            NSLog(@"失败");
        }];
        UIAlertAction *othact = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            NSLog(@"失败");
        }];
        [alert addAction:canact];
        [alert addAction:othact];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

- (void)setButtonToSecondVC
{
    SecondViewController *secondVC = [[SecondViewController alloc]init];
    UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:secondVC];
    [self presentViewController:navC animated:YES completion:^{
        
        
    }];
    [secondVC release];
}

- (void)leftbar
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
